<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/6/4
 * Time: 14:33
 */

namespace app\admin\controller;


use app\admin\model\AdminList;
use think\facade\Session;

class Index extends Base
{



    public function index(){
        $admin_id = Session::get('admin_id');
        $user=AdminList::where('id',$admin_id)->find();
        $this->assign('admin',$user);
        return $this->fetch();
    }

    public function welcome(){
        $admin_id = Session::get('admin_id');
        $user=AdminList::where('id',$admin_id)->find();
        $this->assign('admin',$user);
        return $this->fetch();
    }

    public function pass(){
        if ($this->request->isPost()){
            $post = input('post.');
            $admin = AdminList::where('id',Session::get('admin_id'))->find();
            if ($post['new_password'] !== $post['r_password']){
                return json(['code'=>0,'msg'=>'两次输入密码不一致']);
            }
            if (md5($admin['username'].md5($post['password']).'adminFly')!==$admin['password']){
                return json(['code'=>0,'msg'=>'原密码不正确']);
            }
            if (md5($admin['username'].md5($post['new_password']).'adminFly') == $admin['password']){
                return json(['code'=>0,'msg'=>'新密码不能和原密码相同']);
            }
            $ret = AdminList::update(['password'=>md5($admin['username'].md5($post['new_password']).'adminFly')],['id'=>$admin['id']]);
            if (!$ret){
                return json(['code'=>0,'msg'=>'修改失败']);
            }
            Session::destroy();
            return json(['code'=>1,'msg'=>'修改成功']);
        }
        return $this->fetch();
    }
}